<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class AnchorMigrationTask extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('AnchorMigrationTask')
            ->setDescription('主播迁移任务');
    }

    protected function execute(Input $input, Output $output)
    {
        $lockKey = 'anchor_migration_task_lock';
        $redis = \think\facade\Cache::store('redis');
        
        // 获取分布式锁
        if (!$redis->set($lockKey, time(), ['nx', 'ex' => 300])) {
            $output->writeln('主播迁移任务：其他进程正在执行，跳过-'.date('Y-m-d H:i:s',time()));
            return false;
        }
        
        
        $output->writeln('主播迁移任务：开始执行-'.date('Y-m-d H:i:s',time()));
        //获取当前需执行迁移主播数据
        $migration=\app\common\model\common\AnchorMigration::whereIn('status',[0,1])->order('status','desc')->order('id','asc')->find();
        if(empty($migration['id'])){
            $output->writeln('主播迁移任务：暂无执行数据-'.date('Y-m-d H:i:s',time()));
            return false;
        }
        $migration=$migration->toArray();
        if($migration['status']==1){
            $output->writeln('主播迁移任务：存在迁移中任务-'.date('Y-m-d H:i:s',time()));
            return false;
        }
        $output->writeln('主播迁移任务：获取当前需执行迁移主播数据-'.json_encode($migration,JSON_UNESCAPED_UNICODE).'-'.date('Y-m-d H:i:s',time()));
        //获取待迁出分区数据
        $partitionOutData=\app\common\model\common\PartitionPackage::where([
            'partition_id'=>$migration['migration_out_partition_id'],
            'status'=>1
        ])->find();
        if(empty($partitionOutData['id'])){
            $output->writeln('主播迁移任务：迁出分区ID无效-'.date('Y-m-d H:i:s',time()));
            return false;
        }
        $output->writeln('主播迁移任务：获取待迁出分区数据-'.json_encode($partitionOutData,JSON_UNESCAPED_UNICODE).'-'.date('Y-m-d H:i:s',time()));
        //查询迁出主播用户表相关数据
        $subOutMemberModel=getSwitchDatabaseModel(sprintf("partition_%s",$partitionOutData['partition_id']),$partitionOutData['api_mysql_host_name'],$partitionOutData['api_mysql_database'],$partitionOutData['api_mysql_username'],$partitionOutData['api_mysql_password'],$partitionOutData['api_mysql_hostport']);
        if(!$subOutMemberModel){
            $output->writeln('主播迁移任务：迁出分区数据库连接异常-'.date('Y-m-d H:i:s',time()));
            return false;
        }
        // 修复字段引用错误
        $memberOutData=$subOutMemberModel->table('member')->where([
        'id' => $migration['migration_out_anchor_id'], // 修正字段引用
        'status' =>1
        ])->withoutField(['uuid','device_tokens','identifies'])->find();
        
        // 添加数据验证
        if(empty($memberOutData['id']) || $memberOutData['mobile'] !== $migration['migration_mobile']){
            $output->writeln('主播迁移任务：迁出主播数据验证失败-'.date('Y-m-d H:i:s',time()));
            \app\common\model\common\AnchorMigration::where('id',$migration['id'])->update([
                'status'=>3,
                'error_msg'=>'迁出主播数据验证失败',
                'update_time'=>time()
            ]);
            return false;
        }
        if(empty($memberOutData['id'])){
            $output->writeln('主播迁移任务：迁出分区主播ID无效-'.date('Y-m-d H:i:s',time()));
            return false;
        }
        //获取待迁入分区数据
        $partitionInData=\app\common\model\common\PartitionPackage::where([
            'partition_id'=>$migration['migration_in_partition_id'],
            'status'=>1
        ])->find();
        if(empty($partitionInData['id'])){
            $output->writeln('主播迁移任务：迁入分区ID无效-'.date('Y-m-d H:i:s',time()));
            return false;
        }
        $subInMemberModel=getSwitchDatabaseModel(sprintf("partition_%s",$partitionInData['partition_id']),$partitionInData['api_mysql_host_name'],$partitionInData['api_mysql_database'],$partitionInData['api_mysql_username'],$partitionInData['api_mysql_password'],$partitionInData['api_mysql_hostport']);
        if(!$subInMemberModel){
            $output->writeln('主播迁移任务：迁入分区数据库连接异常-'.date('Y-m-d H:i:s',time()));
            return false;
        }
        //检测迁入分区内该迁移主播号是否存在
        $memberInData=$subInMemberModel->table('member')->where([
            'mobile' => $migration['migration_mobile'],
            'status' =>1
        ])->find();
        if(!empty($memberInData['id'])){
            $output->writeln('主播迁移任务：迁入分区该迁移主播手机号已存在-'.date('Y-m-d H:i:s',time()));
            
            // 更新迁移任务状态为失败
            \app\common\model\common\AnchorMigration::where('id',$migration['id'])->update([
                'status'=>3,
                'error_msg'=>'迁入分区该迁移主播手机号已存在',
                'update_time'=>time()
            ]);
            return false;
        }
        //增加写事务操作
        $subInMemberModel->startTrans();
        $subOutMemberModel->startTrans();
        \think\facade\Db::startTrans(); // 主数据库事务
        try{
            //创建出新增主播ID-member
            $output->writeln('主播迁移任务：创建新增主播ID开始-member-'.date('Y-m-d H:i:s',time()));
            $memberInInsertData=$memberOutData;
            unset($memberInInsertData['id']);
            $memberInInsertData['uuid']=getUUID($partitionInData['partition_id'],1);
            $memberInInsertData['update_time']=time();
            //最新迁入区主播ID
            $newMemberInId=$subInMemberModel->table('member')->insertGetId($memberInInsertData);
            if(!$newMemberInId){
                $output->writeln('主播迁移任务：最新迁入区主播ID创建失败-'.date('Y-m-d H:i:s',time()));
                throw new \think\Exception("最新迁入区主播ID创建失败");
            }
            $output->writeln('主播迁移任务：创建出新增主播ID结束-'.$newMemberInId.date('Y-m-d H:i:s',time()));

            //更新迁移表数据状态
            $ret=\app\common\model\common\AnchorMigration::where('id',$migration['id'])->update([
                'migration_in_anchor_id'=>$newMemberInId,
                'status'=>1,
                'update_time'=>time()
            ]);
            $output->writeln('主播迁移任务：更新迁移表任务状态-'.date('Y-m-d H:i:s',time()));
            if(!$ret){
                $output->writeln('主播迁移任务：更新迁移表任务状态失败-'.date('Y-m-d H:i:s',time()));
                throw new \think\Exception("更新迁移表任务状态失败");
            }
            //同步更新至主播迁移记录中
            $output->writeln('主播迁移任务：已同步更新至主播迁移记录中-'.date('Y-m-d H:i:s',time()));

            //同步主播认证信息-member_auth
            $output->writeln('主播迁移任务：同步主播认证信息开始-member_auth-'.date('Y-m-d H:i:s',time()));
            //查询主播认证信息
            $memberOutAuth=$subOutMemberModel->table('member_auth')->where('member_id',$memberOutData['id'])->find();
            if(!empty($memberOutAuth['id'])){
                $memberInAuth=$memberOutAuth;
                $memberInAuth['member_id']=$newMemberInId;
                $ret=$subInMemberModel->table('member_auth')->insert($memberInAuth);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步主播认证信息新增失败-member_auth-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步主播认证信息新增失败");
                }
                $output->writeln('主播迁移任务：同步主播认证信息-member_auth-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到主播认证信息-member_auth-'.date('Y-m-d H:i:s',time()));
            }

            //同步定期验证表-anchor_periodic_verification
            $output->writeln('主播迁移任务：同步定期验证表开始-anchor_periodic_verification-'.date('Y-m-d H:i:s',time()));
            $anchorOutPeriodicVerifications=$subOutMemberModel->table('anchor_periodic_verification')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($anchorOutPeriodicVerifications)){
                $anchorInPeriodicVerifications=$anchorOutPeriodicVerifications;
                foreach($anchorInPeriodicVerifications as &$anchorInPeriodicVerification){
                    $anchorInPeriodicVerification['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('anchor_periodic_verification')->insertAll($anchorInPeriodicVerifications);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步定期验证表新增失败-anchor_periodic_verification-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步定期验证表新增失败");
                }
                $output->writeln('主播迁移任务：同步定期验证表-anchor_periodic_verification-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步定期验证信息-anchor_periodic_verification-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步更新定期验证表结束-anchor_periodic_verification-'.date('Y-m-d H:i:s',time()));

            //同步定期验证短信发送表-anchor_periodic_verification_sms_record
            $output->writeln('主播迁移任务：同步定期验证短信发送表开始-anchor_periodic_verification_sms_record-'.date('Y-m-d H:i:s',time()));
            $anchorOutPeriodicVerificationSmsRecords=$subOutMemberModel->table('anchor_periodic_verification_sms_record')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($anchorOutPeriodicVerificationSmsRecords)){
                $anchorInPeriodicVerificationSmsRecords=$anchorOutPeriodicVerificationSmsRecords;
                foreach($anchorInPeriodicVerificationSmsRecords as &$anchorInPeriodicVerificationSmsRecord){
                    $anchorInPeriodicVerificationSmsRecord['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('anchor_periodic_verification_sms_record')->insertAll($anchorInPeriodicVerificationSmsRecords);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步定期验证短信发送表新增失败-anchor_periodic_verification_sms_record-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步定期验证短信发送表新增失败");
                }
                $output->writeln('主播迁移任务：同步定期验证短信发送表-anchor_periodic_verification_sms_record-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步定期验证短信发送表信息-anchor_periodic_verification_sms_record-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步更新定期验证短信发送表结束-anchor_periodic_verification_sms_record-'.date('Y-m-d H:i:s',time()));

            //同步客服数据-live_customer_service
            $output->writeln('主播迁移任务：同步客服数据开始-live_customer_service-'.date('Y-m-d H:i:s',time()));
            $liveOutCustomerService=$subOutMemberModel->table('live_customer_service')->where('member_id',$memberOutData['id'])->where('status',1)->find();
            if(!empty($liveOutCustomerService['id'])){
                $liveInCustomerService=$liveOutCustomerService;
                $liveInCustomerService['member_id']=$newMemberInId;
                $ret=$subInMemberModel->table('live_customer_service')->insert($liveInCustomerService);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步客服数据新增失败-member_auth-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步客服数据新增失败");
                }
                $output->writeln('主播迁移任务：同步客服数据-member_auth-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到客服数据-member_auth-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步更新客服数据结束-live_customer_service-'.date('Y-m-d H:i:s',time()));

            //同步高级设置表-live_high_device
            $output->writeln('主播迁移任务：同步高级设置表开始-live_high_device-'.date('Y-m-d H:i:s',time()));
            $liveOutHighDevice=$subOutMemberModel->table('live_high_device')->where('member_id',$memberOutData['id'])->find();
            if(!empty($liveOutHighDevice['id'])){
                $liveInHighDevice=$liveOutHighDevice;
                $liveInHighDevice['member_id']=$newMemberInId;
                $ret=$subInMemberModel->table('live_high_device')->insert($liveInHighDevice);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步高级设置新增失败-member_auth-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步高级设置新增失败");
                }
                $output->writeln('主播迁移任务：同步高级设置数据-member_auth-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到高级设置数据-member_auth-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步高级设置表结束-live_high_device-'.date('Y-m-d H:i:s',time()));

            //同步IM评论颜色表-live_im_font_color
            $output->writeln('主播迁移任务：同步IM评论颜色表开始-live_im_font_color-'.date('Y-m-d H:i:s',time()));
            $liveOutImFontColors=$subOutMemberModel->table('live_im_font_color')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($liveOutImFontColors)){
                $liveInImFontColors=$liveOutImFontColors;
                foreach($liveInImFontColors as &$liveInImFontColor){
                    $liveInImFontColor['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('live_im_font_color')->insertAll($liveInImFontColors);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步IM评论颜色表新增失败-live_im_font_color-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步IM评论颜色表新增失败");
                }
                $output->writeln('主播迁移任务：同步IM评论颜色表-live_im_font_color-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步IM评论颜色表信息-live_im_font_color-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步IM评论颜色表结束-live_im_font_color-'.date('Y-m-d H:i:s',time()));

            //同步主播直播间默认字体显示大小-live_room_font_size
            $output->writeln('主播迁移任务：同步主播直播间默认字体显示大小开始-live_room_font_size-'.date('Y-m-d H:i:s',time()));
            $liveOutRoomFontSizes=$subOutMemberModel->table('live_room_font_size')->where('author_id',$memberOutData['id'])->select()->toArray();
            if(!empty($liveOutRoomFontSizes)){
                $liveInRoomFontSizes=$liveOutRoomFontSizes;
                foreach($liveInRoomFontSizes as &$liveInRoomFontSize){
                    $liveInRoomFontSize['author_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('live_room_font_size')->insertAll($liveInRoomFontSizes);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步主播直播间默认字体显示大小表新增失败-live_room_font_size-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步主播直播间默认字体显示大小表新增失败");
                }
                $output->writeln('主播迁移任务：同步主播直播间默认字体显示大小-live_room_font_size-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步主播直播间默认字体显示大小表信息-live_room_font_size-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步主播直播间默认字体显示大小结束-live_room_font_size-'.date('Y-m-d H:i:s',time()));

            //同步快捷语设置-live_shortcut
            $output->writeln('主播迁移任务：同步快捷语设置开始-live_shortcut-'.date('Y-m-d H:i:s',time()));
            $liveOutShortcuts=$subOutMemberModel->table('live_shortcut')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($liveOutShortcuts)){
                $liveInShortcuts=$liveOutShortcuts;
                foreach($liveInShortcuts as &$liveInShortcut){
                    $liveInShortcut['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('live_shortcut')->insertAll($liveInShortcuts);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步快捷语设置表新增失败-live_shortcut-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步快捷语设置表新增失败");
                }
                $output->writeln('主播迁移任务：同步快捷语设置-live_shortcut-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步快捷语设置表信息-live_shortcut-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步快捷语设置结束-live_shortcut-'.date('Y-m-d H:i:s',time()));

            //同步主播直播线路限制-member_live_line_restriction
            $output->writeln('主播迁移任务：同步主播直播线路限制开始-member_live_line_restriction-'.date('Y-m-d H:i:s',time()));
            $memberOutLiveLineRestrictions=$subOutMemberModel->table('member_live_line_restriction')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($memberOutLiveLineRestrictions)){
                $memberInLiveLineRestrictions=$memberOutLiveLineRestrictions;
                foreach($memberInLiveLineRestrictions as &$memberInLiveLineRestriction){
                    $memberInLiveLineRestriction['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('member_live_line_restriction')->insertAll($memberInLiveLineRestrictions);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步主播直播线路限制表新增失败-member_live_line_restriction-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步主播直播线路限制表新增失败");
                }
                $output->writeln('主播迁移任务：同步主播直播线路限制-member_live_line_restriction-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步主播直播线路限制表信息-member_live_line_restriction-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步主播直播线路限制结束-member_live_line_restriction-'.date('Y-m-d H:i:s',time()));
            
            //同步加人气设置表-member_live_popularity
            $output->writeln('主播迁移任务：同步加人气设置表开始-member_live_popularity-'.date('Y-m-d H:i:s',time()));
            $memberOutLivePopularities=$subOutMemberModel->table('member_live_popularity')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($memberOutLivePopularities)){
                $memberInLivePopularities=$memberOutLivePopularities;
                foreach($memberInLivePopularities as &$memberInLivePopularity){
                    $memberInLivePopularity['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('member_live_popularity')->insertAll($memberInLivePopularities);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步加人气设置表新增失败-member_live_popularity-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步加人气设置表新增失败");
                }
                $output->writeln('主播迁移任务：同步加人气设置表-member_live_popularity-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步加人气设置表信息-member_live_popularity-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步加人气设置表结束-member_live_popularity-'.date('Y-m-d H:i:s',time()));
            
            //同步流量余额订单表-member_orders
            $output->writeln('主播迁移任务：同步流量余额订单表开始-member_orders-'.date('Y-m-d H:i:s',time()));
            $memberOutOrders=$subOutMemberModel->table('member_orders')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($memberOutOrders)){
                $memberInOrders=$memberOutOrders;
                foreach($memberInOrders as &$memberInOrder){
                    $memberInOrder['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('member_orders')->insertAll($memberInOrders);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步流量余额订单表新增失败-member_orders-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步流量余额订单表新增失败");
                }
                $output->writeln('主播迁移任务：同步流量余额订单表-member_orders-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步流量余额订单表信息-member_orders-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步流量余额订单表结束-member_orders-'.date('Y-m-d H:i:s',time()));
            
            //同步直播码率限制-member_resolution_ratio
            $output->writeln('主播迁移任务：同步直播码率限制开始-member_resolution_ratio-'.date('Y-m-d H:i:s',time()));
            $memberOutResolutionRatios=$subOutMemberModel->table('member_resolution_ratio')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($memberOutResolutionRatios)){
                $memberInResolutionRatios=$memberOutResolutionRatios;
                foreach($memberInResolutionRatios as &$memberInResolutionRatio){
                    $memberInResolutionRatio['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('member_resolution_ratio')->insertAll($memberInResolutionRatios);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步直播码率限制表新增失败-member_resolution_ratio-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步直播码率限制表新增失败");
                }
                $output->writeln('主播迁移任务：同步直播码率限制-member_resolution_ratio-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步直播码率限制表信息-member_resolution_ratio-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步直播码率限制结束-member_resolution_ratio-'.date('Y-m-d H:i:s',time()));
            
            //同步用户标签表-member_tags
            $output->writeln('主播迁移任务：同步用户标签表开始-member_tags-'.date('Y-m-d H:i:s',time()));
            $memberOutTags=$subOutMemberModel->table('member_tags')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($memberOutTags)){
                $memberInTags=$memberOutTags;
                foreach($memberInTags as &$memberInTag){
                    $memberInTag['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('member_tags')->insertAll($memberInTags);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步用户标签表新增失败-member_tags-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步用户标签表新增失败");
                }
                $output->writeln('主播迁移任务：同步用户标签表-member_tags-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步用户标签表信息-member_tags-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步用户标签表结束-member_tags-'.date('Y-m-d H:i:s',time()));
            
            //同步主播信息提示状态表-member_tips
            $output->writeln('主播迁移任务：同步主播信息提示状态表开始-member_tips-'.date('Y-m-d H:i:s',time()));
            $memberOutTips=$subOutMemberModel->table('member_tips')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($memberOutTips)){
                $memberInTips=$memberOutTips;
                foreach($memberInTips as &$memberInTip){
                    $memberInTip['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('member_tips')->insertAll($memberInTips);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步主播信息提示状态表新增失败-member_tips-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步主播信息提示状态表新增失败");
                }
                $output->writeln('主播迁移任务：同步主播信息提示状态表-member_tips-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步主播信息提示状态表信息-member_tips-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步主播信息提示状态表结束-member_tips-'.date('Y-m-d H:i:s',time()));
            
            //同步VIP充值订单表-member_vip_orders
            $output->writeln('主播迁移任务：同步VIP充值订单表开始-member_vip_orders-'.date('Y-m-d H:i:s',time()));
            $memberOutVipOrders=$subOutMemberModel->table('member_vip_orders')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($memberOutVipOrders)){
                $memberInVipOrders=$memberOutVipOrders;
                foreach($memberInVipOrders as &$memberInVipOrder){
                    $memberInVipOrder['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('member_vip_orders')->insertAll($memberInVipOrders);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步VIP充值订单表新增失败-member_vip_orders-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步VIP充值订单表新增失败");
                }
                $output->writeln('主播迁移任务：同步VIP充值订单表-member_vip_orders-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步VIP充值订单表信息-member_vip_orders-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步VIP充值订单表结束-member_vip_orders-'.date('Y-m-d H:i:s',time()));
            
            //同步用户钱包表-member_wallet
            $output->writeln('主播迁移任务：同步用户钱包表开始-member_wallet-'.date('Y-m-d H:i:s',time()));
            $memberOutWallet=$subOutMemberModel->table('member_wallet')->where('member_id',$memberOutData['id'])->find();
            if(!empty($memberOutWallet['id'])){
                $memberInWallet=$memberOutWallet;
                $memberInWallet['member_id']=$newMemberInId;
                $ret=$subInMemberModel->table('member_wallet')->insert($memberInWallet);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步用户钱包表新增失败-member_wallet-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步用户钱包表新增失败");
                }
                $output->writeln('主播迁移任务：同步用户钱包表-member_wallet-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步用户钱包表信息-member_wallet-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步用户钱包表结束-member_wallet-'.date('Y-m-d H:i:s',time()));
            
            //同步用户钱包日志明细表-member_wallet_log
            $output->writeln('主播迁移任务：同步用户钱包日志明细表开始-member_wallet_log-'.date('Y-m-d H:i:s',time()));
            $memberOutWalletLogs=$subOutMemberModel->table('member_wallet_log')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($memberOutWalletLogs)){
                $memberInWalletLogs=$memberOutWalletLogs;
                foreach($memberInWalletLogs as &$memberInWalletLog){
                    $memberInWalletLog['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('member_wallet_log')->insertAll($memberInWalletLogs);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步用户钱包日志明细表新增失败-member_wallet_log-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步用户钱包日志明细表新增失败");
                }
                $output->writeln('主播迁移任务：同步用户钱包日志明细表-member_wallet_log-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步用户钱包日志明细表信息-member_wallet_log-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步用户钱包日志明细表结束-member_wallet_log-'.date('Y-m-d H:i:s',time()));
            
            //同步验证表-new_anchor_verification
            $output->writeln('主播迁移任务：同步验证表开始-new_anchor_verification-'.date('Y-m-d H:i:s',time()));
            $newOutAnchorVerifications=$subOutMemberModel->table('new_anchor_verification')->where('member_id',$memberOutData['id'])->select()->toArray();
            if(!empty($newOutAnchorVerifications)){
                $newInAnchorVerifications=$newOutAnchorVerifications;
                foreach($newInAnchorVerifications as &$newInAnchorVerification){
                    $newInAnchorVerification['member_id']=$newMemberInId;
                }
                $ret=$subInMemberModel->table('new_anchor_verification')->insertAll($newInAnchorVerifications);
                if(!$ret){
                    $output->writeln('主播迁移任务：同步验证表新增失败-new_anchor_verification-'.date('Y-m-d H:i:s',time()));
                    throw new \think\Exception("同步验证表新增失败");
                }
                $output->writeln('主播迁移任务：同步验证表-new_anchor_verification-'.date('Y-m-d H:i:s',time()));
            }else{
                $output->writeln('主播迁移任务：未检测到同步验证表信息-new_anchor_verification-'.date('Y-m-d H:i:s',time()));
            }
            $output->writeln('主播迁移任务：同步验证表结束-new_anchor_verification-'.date('Y-m-d H:i:s',time()));
            // 将关键状态更新纳入事务
            $ret=$subOutMemberModel->table('member')->where('id',$migration['migration_out_anchor_id'])->update([
                'status'=>4,
                'update_time'=>time()
            ]);
            if(!$ret){
                throw new \think\Exception("迁出分区主播状态更新失败");
            }
            
            $ret=\app\common\model\common\Anchor::where('id',$migration['anchor_platform_id'])->update([
                'partition_id'=>$migration['migration_in_partition_id'],
                'update_time'=>time()
            ]);
            if(!$ret){
                throw new \think\Exception("主表主播分区指向更新失败");
            }
            
            // 最后更新迁移任务状态为完成
            $ret=\app\common\model\common\AnchorMigration::where('id',$migration['id'])->update([
                'status'=>2,
                'update_time'=>time()
            ]);
            if(!$ret){
                throw new \think\Exception("更新迁移任务完成状态失败");
            }
            
            // 提交所有事务
            $subInMemberModel->commit();
            $subOutMemberModel->commit();
            \think\facade\Db::commit();
        }catch(\Throwable $e){
            $output->writeln('主播迁移任务：主播迁移任务出错-'.$e->getMessage().'-'.date('Y-m-d H:i:s',time()));
            //回滚所有写入操作
            $subInMemberModel->rollback();
            $subOutMemberModel->rollback();
            \think\facade\Db::rollback();
            // 更新迁移任务状态为失败
            \app\common\model\common\AnchorMigration::where('id',$migration['id'])->update([
                'status'=>3,
                'error_msg'=>'全局捕捉出错：'.$e->getMessage(),
                'update_time'=>time()
            ]);
            return false;
        }
        $output->writeln('主播迁移任务：主播迁移任务结束执行-'.date('Y-m-d H:i:s',time()));
        // 释放锁
        $redis->del($lockKey);
        return true;
    }
   
}
